package com.sh.weblogspringboot.admin.service.impl;

import com.sh.weblogspringboot.admin.service.AdminFileService;
import com.sh.weblogspringboot.admin.util.MinioUtil;
import com.sh.weblogspringboot.admin.vo.file.UploadFileRspVO;
import com.sh.weblogspringboot.common.enums.ResponseCodeEnum;
import com.sh.weblogspringboot.common.exception.BizException;
import com.sh.weblogspringboot.common.util.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author songHai
 * @Classname AdminFileService
 * @Description
 * @Date 2025/4/1 23:00
 */
@Service
@Slf4j
public class AdminFileServiceImpl implements AdminFileService {

    @Autowired
    private MinioUtil minioUtil;

    @Override
    public Response uploadFile(MultipartFile file) {
        try {
            // 上传文件
            String url = minioUtil.uploadFile(file);

            // 构建成功反参数
            return Response.success("上传成功", UploadFileRspVO.builder().url(url).build());
        } catch (Exception e) {
            log.error("===》 上传文件至Minio失败:", e);
            throw new BizException(ResponseCodeEnum.FILE_UPLOAD_FAILED);
        }
    }
}
